package com.lagou.edu.mvcframework.pojo;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;

/**
 * @author yuting.cai
 */
public class SecurityCheck {

    public String[] getNames() {
        return names;
    }

    public void setNames(String[] names) {
        this.names = names;
    }

    private String[] names = new String[0];

    private static final String NAME_PREFIX = "username";

    private String getNameFromRequest(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        for(Map.Entry<String, String[]> entry : parameterMap.entrySet()){
            System.out.println("key: "+entry.getKey()+"  value: "+ Arrays.toString(entry.getValue()));
        }

        if(parameterMap.isEmpty()){
            return "";
        }else {
            String[] names = parameterMap.get(NAME_PREFIX);
            if(names.length == 0){
                return "";
            }else {
                //只返回第一个用户名， 不支持同时录入多个用户名
                return names[0];
            }
        }
    }


    public Boolean securityCheck(HttpServletRequest request){
        String nameFromRequest = getNameFromRequest(request);
        System.out.println("security check");

        System.out.println(nameFromRequest);
        System.out.println(Arrays.toString(names));
        for (String s : names) {
            System.out.println(s);
            if (s.equals(nameFromRequest)) {
                System.out.println("通过了哦！");
                return true;
            }
        }
        System.out.println("没通过！");
        return false;
    }



}
